package sprites;

import game.environments.GameEnvironment;
import java.awt.Point;
import java.awt.event.KeyEvent;

/**
 * Class of sprites controlled by user input.
 * @author Amay
 *
 */
public class ResponsiveSprite extends Sprite {

    private int myRightKey;
    private int myLeftKey;
    private int myUpKey;
    private int myDownKey;
    private int myWeaponLaunchKey;
    private int myDX;
    private int myDY;
    private int dx;
    private int dy;
    private AllySprite myWeapon;

    private final int MOVEMENT_SPEED = 10;

    /**
     * Constructor that calls super class.
     */
    public ResponsiveSprite () {
        super();
    }

    /**
     * Constructor that sets sprite image from String image location.
     * @param imgLoc String location of sprite image.
     */
    public ResponsiveSprite (String imgLoc) {
        super(imgLoc);
    }

    /**
     * Constructor that creates a new Sprite of the same parameters as the passed-in Sprite.
     * Also includes weapon used by respSprite.
     * @param respSprite ResponsiveSprite to be copied
     */
    public ResponsiveSprite (ResponsiveSprite respSprite) {
        super(respSprite);
        myWeapon = new AllySprite(respSprite.getWeapon());
    }

    @Override
    public void update () {
        Point currentPos = getPosition();
        currentPos.setLocation(currentPos.getX() + myDX, currentPos.getY() + myDY);
        configurePosition(currentPos);
        myDX = 0;
        myDY = 0;
    }

    @Override
    public void collisionHandle (Sprite s) {
        if (isCollidableWith(s)) {
            int changeInHP = 0;
            if (s instanceof AllySprite) {
                changeInHP = 1;
            }
            if (s instanceof EnemySprite) {
                changeInHP = -1;
            }
            configureHP(getHP() + changeInHP);
        }
    }

    /**
     * Sets the input controls that will control the movement of the sprite.
     * @param up int that represent the key to move sprite upward
     * @param down int that represent the key to move sprite downward
     * @param left int that represent the key to move sprite left
     * @param right int that represent the key to move sprite upward
     */
    public void configureInput (int up, int down, int left, int right) {
        myUpKey = up;
        myDownKey = down;
        myLeftKey = left;
        myRightKey = right;
    }


    /**
     * Sets type of sprite that will be used as a weapon by the current sprite.
     * @param weapon EnemySprite that will act as a weapon
     */
    public void configureWeapon (AllySprite weapon) {
        myWeapon = weapon;
    }

    /**
     * Sets the key that will launch the sprite's weapon.
     * @param launchKey int that represents the key to launch the weapon.
     */
    public void configureWeapon (int launchKey) {
        myWeaponLaunchKey = launchKey;
    }

    /**
     * Returns the AllySprite currently being used as a weapon.
     * @return myWeapon AllySprite being used as a weapon
     */
    public AllySprite getWeapon () {
        return myWeapon;
    }

    /**
     * Changes movement variables in the gameenvironment based on the key input.
     * @param e KeyEvent from key being pressed
     * @param ge GameEnvironment in which Sprite lives
     */
    public void keyInputPort (KeyEvent e, GameEnvironment ge) {

        int inputCode = e.getKeyCode();
        if (inputCode == myRightKey) {
            myDX = MOVEMENT_SPEED;
        }
        if (inputCode == myLeftKey) {
            myDX = -MOVEMENT_SPEED;
        }
        if (inputCode == myUpKey) {
            myDY = -MOVEMENT_SPEED;
        }
        if (inputCode == myDownKey) {
            myDY = MOVEMENT_SPEED;
        }
        if (inputCode == myWeaponLaunchKey) {
            launchWeapon(ge);
        }
    }

    private void launchWeapon (GameEnvironment ge) {
        AllySprite weapon = new AllySprite(myWeapon);
        weapon.configurePosition(new Point(getPosition()));
        ge.add(weapon);
    }
}
